package top.hcode.hoj.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.hcode.hoj.common.result.CommonResult;
import top.hcode.hoj.pojo.dto.BaseIdDTO;
import top.hcode.hoj.pojo.dto.org.QueryClassDTO;
import top.hcode.hoj.pojo.dto.org.QueryGradeDTO;
import top.hcode.hoj.pojo.entity.org.Org;
import top.hcode.hoj.pojo.form.AdmQueryOrgForm;
import top.hcode.hoj.pojo.vo.org.ClassVO;
import top.hcode.hoj.pojo.vo.org.GradeVO;
import top.hcode.hoj.pojo.vo.org.SchoolVO;
import top.hcode.hoj.service.UserAuthService;
import top.hcode.hoj.service.admin.AdminOrgService;

import java.util.List;

/**
 * @author orangej
 * @since 2025-01-21
 */
@Slf4j
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class AdminOrgController {

    private final AdminOrgService orgService;
    private final UserAuthService userAuthService;

    @ApiOperation(value = "获取学校列表")
    @GetMapping("/admin/org/top-school/options")
    @RequiresAuthentication
    public CommonResult<List<SchoolVO>> getTopSchoolList() {
        List<String> schoolIdList = userAuthService.getSchoolIdList();
        return CommonResult.successResponse(orgService.getTopSchoolVOList(schoolIdList));
    }

    @ApiOperation(value = "获取学校列表（校区）")
    @GetMapping("/admin/org/school/list")
    @RequiresAuthentication
    public CommonResult<List<SchoolVO>> getSchoolList() {
        return CommonResult.successResponse(orgService.getCampusVOList(userAuthService.getSchoolIdList()));
    }

    @ApiOperation(value = "获取年级列表")
    @GetMapping("/admin/org/grade/list")
    @RequiresAuthentication
    public CommonResult<List<GradeVO>> getGradeList(@Validated QueryGradeDTO queryGradeDTO) {
        userAuthService.checkOrgId(queryGradeDTO.getSchoolId());
        return CommonResult.successResponse(orgService.getGradeList(queryGradeDTO));
    }

    @ApiOperation(value = "获取班级列表")
    @GetMapping("/admin/org/class/list")
    @RequiresAuthentication
    public CommonResult<List<ClassVO>> getClassList(QueryClassDTO queryClassDTO) {
        userAuthService.checkOrgId(queryClassDTO.getSchoolId());
        userAuthService.checkOrgId(queryClassDTO.getGradeId());
        return CommonResult.successResponse(orgService.getClassList(queryClassDTO));
    }

    @ApiOperation(value = "获取祖先组织列表")
    @GetMapping("/admin/org/ancestor/list")
    @RequiresAuthentication
    public CommonResult<List<Org>> getAncestorOrgList(String orgId) {
        return CommonResult.successResponse(orgService.getAncestorOrgList(orgId));
    }

    @GetMapping("/admin/org/list")
    @RequiresAuthentication
    public CommonResult<IPage<Org>> getOrgList(AdmQueryOrgForm form) {
        return CommonResult.successResponse(orgService.getOrgList(form));
    }

    @PostMapping("/admin/org/save")
    @RequiresAuthentication
    public CommonResult<Org> saveOrg(@RequestBody Org org) {
        return CommonResult.successResponse(orgService.saveOrg(org));
    }

    @PostMapping("/admin/org/delete")
    @RequiresAuthentication
    public CommonResult<Void> deleteOrg(@RequestBody BaseIdDTO dto) {
        orgService.deleteOrg(dto.getId());
        return CommonResult.successResponse();
    }

}
